package com.fh.module_main.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.fh.lib_base.util.AppUtils
import com.fh.module_main.R

class FastIndexView(context: Context,attributeSet: AttributeSet): View(context,attributeSet) {

    private val INDEX_NAME = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    private lateinit var listener: OnLetterUpdateListener
    private var mPaint: Paint
    private var cellHeight: Float = 0.0f
    private var viewWidth: Float = 0.0f
    private var touchIndex = -1
    private var normalColor:Int = 0
    private var selectedBgColor:Int = 0
    private var selectedFontColor:Int = 0

    init {
        mPaint = Paint()
        mPaint.textSize = AppUtils.dp2px(12.0f)
        mPaint.isAntiAlias = true
        //获取文字被选中的颜色
        normalColor = resources.getColor(R.color.edittext_textcolor)
        selectedBgColor = resources.getColor(R.color.hotel_theme)
        selectedFontColor = resources.getColor(R.color.white)
    }

    override fun onDraw(canvas: Canvas) {
        for (i in INDEX_NAME.indices){
            var text = INDEX_NAME.substring(i, i + 1)
            //计算绘制字符的X方向起点
            var x = viewWidth / 2.0f - mPaint.measureText(text) / 2.0f
            var bounds = Rect();
            mPaint.getTextBounds(text,0,text.length,bounds)
            var textHeight = bounds.height()
            //计算绘制字符的Y方向起点
            var y = cellHeight / 2.0f + textHeight / 2.0f + i * cellHeight
            //选中
            if(touchIndex == i){
                mPaint.setColor(selectedBgColor)
//                mPaint.typeface = Typeface.DEFAULT_BOLD
                //画选中圆形背景
                canvas.drawCircle(x+mPaint.measureText(text)/2,y-textHeight/2,22.0f,mPaint)
                mPaint.setColor(selectedFontColor)
            }else{
                mPaint.setColor(normalColor)
                mPaint.typeface = Typeface.DEFAULT
            }
            canvas.drawText(text, x, y, mPaint)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        var index = 0
        when(event.getAction()){
            MotionEvent.ACTION_DOWN -> {
                //计算当前触摸的字符索引
                index = (event.getY() / cellHeight).toInt()
                if (index >= 0 && index < INDEX_NAME.length) {
                    if (listener != null) {
                        listener.onLetterUpdate(INDEX_NAME.substring(index, index + 1))
                    }
                    touchIndex = index;
                }
            }
            MotionEvent.ACTION_MOVE -> {
                //计算当前触摸的字符索引
                index = (event.getY() / cellHeight).toInt()
                if (index >= 0 && index < INDEX_NAME.length) {
                    if (index != touchIndex) {
                        if (listener != null) {
                            listener.onLetterUpdate(INDEX_NAME.substring(index, index + 1))
                        }
                        touchIndex = index;
                    }
                }
            }
        }
        invalidate();
        return true;
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //得到当前控件的宽度
        viewWidth = measuredWidth.toFloat()
        var mHeight = measuredHeight
        //获取单个字符能够拥有的高度
        cellHeight = mHeight * 1.0f / INDEX_NAME.length
    }

    interface OnLetterUpdateListener {
        fun onLetterUpdate(letter: String?)
    }

    fun setListener(listener: OnLetterUpdateListener?) {
        this.listener = listener!!
    }
    //监听RecyclerView滑动，变化字母选中
    fun setLetter(index: String){
        touchIndex = INDEX_NAME.indexOf(index.uppercase())
        invalidate()
    }
}